from core.rest_client_new import RestClient
from utils.AssertUtil import AssertUtil
from utils.ExtractUtil import ExtractUtil


class ApiService:
    def __init__(self):
        self.session = RestClient()
        self.extract = ExtractUtil()

    def handle_case(self, test_data):
        # 获取url
        url = self.extract.extract_url(test_data['request_info']["url"])
        # 获取method
        method = test_data['request_info']["method"]
        # 获取headers
        headers = test_data['request_info']["headers"]
        headers = self.extract.extract_case(headers)
        # 获取case_info
        case_info = test_data['case_info']
        # 获取validate
        validate = case_info.pop("validate", None)
        # 获取extract
        extract = case_info.pop("extract", None)
        case_info = self.extract.extract_case(case_info)
        res= self.session.do_request(url=url,method=method,headers=headers,**case_info)
        #print(res)
        #将extract写入yaml
        self.extract.extract_data(res, extract)
        #断言逻辑
        AssertUtil().validate_response(res, validate)
        return res
